using AutomatedTrading.Data.Entity;
using Automation.Data.Helper;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using MySqlHelper = Automation.Data.Helper.MySqlHelper;

namespace AutomatedTrading.Data.Service
{
    public class AccountLinkRepository
    {
        MySqlHelper _mySqlHelper;
        public AccountLinkRepository()
        {
            _mySqlHelper = new MySqlHelper();
        }
        public AccountLinkRepository(MySqlHelper mySqlHelper)
        {
            _mySqlHelper = mySqlHelper;
        }

        public async Task<List<AccountLink>> GetAllAsync()
        {
            var sql = "SELECT * FROM accountlink";
            var dt = await Task.Run(() => _mySqlHelper.ExecuteDataTable(sql));
            List<AccountLink> result = JsonHelper.DataTableToEntityArray<AccountLink>(dt);
            return result;
        }

        public async Task<AccountLink> GetByIdAsync(int id)
        {
            var sql = "SELECT * FROM accountlink WHERE ID = @ID";
            var param = new MySqlParameter("@ID", id);
            var dt = await Task.Run(() => _mySqlHelper.ExecuteDataTable(sql, param));
            List<AccountLink> result = JsonHelper.DataTableToEntityArray<AccountLink>(dt);
            return result.Count > 0 ? result[0] : null;
        }

        public async Task<AccountLink> GetByTargetNameAsync(string targetName)
        {
            var sql = "SELECT * FROM accountlink WHERE targetName = @TargetName";
            var param = new MySqlParameter("@TargetName", targetName);
            var dt = await Task.Run(() => _mySqlHelper.ExecuteDataTable(sql, param));
            List<AccountLink> result = JsonHelper.DataTableToEntityArray<AccountLink>(dt);
            return result.Count > 0 ? result[0] : null;
        }



    }
}